package cn.tom.tools;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;

/**
 * @author 酒辞
 */
public class SmsUtil {
    private final static String SECRET_ID ="AKIDIXe4PZYRBtSr5QpVRJADcx9FCKSTanxD";
    private final static String SECRET_KEY = "eBWt9GRfkcNwbHtTzbFrdSGza2Mg1MtL";
    private final static String TEMPLATE_ID ="2152443";
    private final static String SDK_APP_ID ="1400897317";
    private final static String SIGN_NAME ="小辞i公众号";
    private final static String SESSION_CONTEXT ="小辞短信验证";

    public static boolean sendMessage(String phone,String code){
        Credential cred = new Credential(SECRET_ID,SECRET_KEY);
        SmsClient client = new SmsClient(cred,"ap-guangzhou");
        SendSmsRequest request = new SendSmsRequest();
        SendSmsResponse response = null;
        request.setSmsSdkAppId(SDK_APP_ID);
        request.setSignName(SIGN_NAME);
        request.setSessionContext(SESSION_CONTEXT);
        request.setTemplateId(TEMPLATE_ID);
        request.setPhoneNumberSet(new String[]{"+86"+phone});
        request.setTemplateParamSet(new String[]{code});
        try {
            response = client.SendSms(request);
        } catch (TencentCloudSDKException e) {
            throw new RuntimeException(e);
        }
        String str = SendSmsResponse.toJsonString(response);
        System.out.println("str"+str);
        String requestId = response.getRequestId();
        System.out.println("requestId"+requestId);
        return true;
    }
}
